/* eslint-disable max-lines */
import TauSimpleSelect from '@/components/TauSimpleSelect';
import { ellipsisRender } from '@/components/TauTable';
import * as BASE from '@/stores/definitions-base';
import { Input, Switch, DatePicker } from '@alifd/next';
import { t } from '@lingui/macro';
import React, { } from 'react';

const {
  DATETIME_TEXT,
  DATE_TEXT,
  TIME_TEXT,
  EMPTY_TEXT,
  ENUMS: ENUMSBase,
  FieldsGrid: FieldGridBase,
  TitleGrid,
  column: columnBase,
  toSelectDataSource,
} = BASE;
export { DATETIME_TEXT, DATE_TEXT, EMPTY_TEXT, TIME_TEXT, TitleGrid, toSelectDataSource };

/**
 * @param {*} path        属性path
 * @param {*} props       column属性
 * @returns column定义
 */
export function column(...args) {
  return columnBase(ticketModal, ...args);
}

/**
 * 显示一组表单字段
 * ```js
 * {
 *   fields: string[],    // 字段名称，可以使用.来指定嵌套对象属性，例如order.busOrder.shift
 *   columns: number,     // 列数，默认为4,最大为12,最小为1
 *   model: object,       // 字段定义数据模型的根对象，默认不指定，仅在使用子对象作为根模型时设置
 *   overrideModel: object, // 重载字段定义数据模型
 *   mode: string,        // 表单模式，默认为edit，可选view
 *   itemLayout: object,  // FormItem布局配置，默认为{}
 * }
 * ```
 *
 * @param {object} param0
 * @returns
 */
export function FieldsGrid(props) {
  return <FieldGridBase model={ticketModal} {...props} />;
}

//----------------------------------------------------------------------
export const ENUMS = {
  ...ENUMSBase,
  // 1-包车订单 2-专线订单 3-巴士订单
  orderType: {
    2: t`专线订单`,
    3: t`巴士订单`,
  },
  isReturn: {
    0: t`去程`,
    1: t`返程`,
  },
  refundStatus: {
    0: t`未退款`,
    1: t`已退款`,
  },
  processingState: {
    0: t`已驳回`,
    1: t`未处理`,
    2: t`处理中`,
    3: t`已处理`,
    4: t`已撤销`,
  },
  applicationRefundState: {
    '-1': t`已取消`,
    0: t`已驳回`,
    1: t`申请中`,
    2: t`已提交`,
  },
};

/**
 * 模型定义：
 * 1. 普通文本仅需要定义label
 * 2. 需要通过枚举转换的值，定义text; 当使用renderer返回组件时，text属性无效，需要通过组件的renderPreview来指定预览时的绘制方式
 * 3. 通用的ui属性需要定制时，定义ui; 特定模式(view, edit, add)下的ui，添加后缀定义为ui_<mode>，例如：ui_view, ui_edit, ui_add, ui_table
 * 4. ui可配置属性包括:
 *    renderer: 内容渲染函数, Form表单中的原型为：(value: any, formData: any, fieldDefinition: any) => ReactNode; Table Column中的原型为: (value: any, columnIndex: number, rowData: any) => ReactNode;
 *    itemProps: 表单Form.Item的属性，可以是对象，或函数({ isPreview: boolean, mode: string, value: any, formData: any, ...otherDefinitions}) => void;
 *    span: Grid Cell占用的列数, 默认为1
 *
 * 【注】获取接口的model定义后，vscode中通过正则替换：^(\s+)"label": "([^']+)"  ===> $1label: t`$2`
 */
export const ticketModal = {
  orderType: {
    label: t`订单类型`,
    text: (v) => ENUMS.orderType[v],
  },
  ticketCode: {
    label: t`车票号`,
  },
  isReturn: {
    label: t`往返程`,
    text: (v) => ENUMS.isReturn[v],
  },
  orderId: {
    label: t`订单id`,
  },
  startMainArea: {
    label: t`开始城市`,
  },
  startMinorArea: {
    label: t`开始站点id`,
  },
  startMinorAreaText: {
    label: t`开始站点名称`,
  },
  endMainArea: {
    label: t`结束城市`,
  },
  endMinorArea: {
    label: t`结束站点id`,
  },
  endMinorAreaText: {
    label: t`结束站点名称`,
  },
  passengerName: {
    label: t`乘客姓名`,
  },
  price: {
    label: t`票价`,
    text: (v) => <span>{v?.toFixed(2)}</span>,
  },
  payTime: {
    label: t`支付时间`,
    text: DATETIME_TEXT,
  },
  shiftTime: {
    label: t`出发时间`,
    text: DATETIME_TEXT,
  },
  operatingState: {
    label: t`运营商状态`,
  },
  operatorRefundTime: {
    label: t`运营商退款时间`,
    text: DATETIME_TEXT,
  },
  ticketState: {
    label: t`车票状态`,
  },
  tenantId: {
    label: t`租户ID`,
  },
  routeText: {
    label: t`线路`,
  },
  id: {
    label: '',
  },
  // 新增字段
  applicationTime: {
    label: t`平台申请退票时间`,
    text: DATETIME_TEXT,
  },
  processingState: {
    label: t`运营商处理状态`,
    text: (v, r) => {
      if (global?.getEnvCfg()?.companies.ZL.id === r.companyId) {
        return <>{'无'}</>;
      } else if (v === '0') {
        const { rejectReason = '无' } = r || {};
        return ellipsisRender(`${ENUMS.processingState[v]} (${rejectReason})`);
      } else {
        return <>{ENUMS.processingState[v]}</>;
      }
    },
  },
  refundTime: {
    label: t`平台退款时间`,
    text: DATETIME_TEXT,
  },
  rejectReason: {
    label: t`驳回原因`,
  },
  applicationRefundState: {
    label: t`平台退票状态`,
    text: (v) => ENUMS.applicationRefundState[v],
  },
  companyName: {
    label: t`承运公司`,
    text: (v, r) => rootStore.ticketStore._COMPANYS[r.companyId],
  },
};
